﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using ScormModel;
using ScormModel.View;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using ScormModel.HtmlBuilder;

namespace ScormModel.Question
{
    public class SummaryData
    {
        private List<string> listObjective;
        string _identifier = "item";
        public List<string> ListObjective
        {
            get { return this.listObjective; }
            set { this.listObjective = value; }
        }

        public HtmlDocument CreateHtmlNode()
        {
            ScormModel.HtmlBuilder.HtmlElement inputForm = new ScormModel.HtmlBuilder.HtmlElement("form");
            inputForm.Nodes.Clear();
            HtmlAttribute nameAttribute = new HtmlAttribute("name", this._identifier);
            inputForm.Attributes.Add(nameAttribute);
            //input data here
            this.WriteSummmaryHtmlCode(inputForm);
            this.WriteJavaScriptHtmlCode(inputForm);

            StreamReader sr = new StreamReader(Directory.GetCurrentDirectory() + "\\QuestionTemplates\\" + "QuestionTemplatePage.htm", System.Text.UnicodeEncoding.UTF8);

            string readHtmlCode = sr.ReadToEnd();
            sr.Close();
            ScormModel.HtmlBuilder.HtmlDocument mDocument = ScormModel.HtmlBuilder.HtmlDocument.Create(readHtmlCode, true);//sr.ReadToEnd(),true);

            ScormModel.HtmlBuilder.HtmlElement htmlElement = (ScormModel.HtmlBuilder.HtmlElement)mDocument.Nodes["HTML"];
            ScormModel.HtmlBuilder.HtmlElement questionElement = (ScormModel.HtmlBuilder.HtmlElement)htmlElement.Nodes["BODY"];
            questionElement.Nodes.Add(inputForm);
            return mDocument;
        }

        private void WriteSummmaryHtmlCode(ScormModel.HtmlBuilder.HtmlElement inputForm)
        {
            ScormModel.HtmlBuilder.HtmlElement mainTableElement = new ScormModel.HtmlBuilder.HtmlElement("table");
            for (int i = 0; i < listObjective.Count; i++)
            {

                ScormModel.HtmlBuilder.HtmlElement row1Elelemt = new ScormModel.HtmlBuilder.HtmlElement("tr");
                ScormModel.HtmlBuilder.HtmlElement data1Elelemt = new ScormModel.HtmlBuilder.HtmlElement("td");

                ScormModel.HtmlBuilder.HtmlElement descriptionElement = new ScormModel.HtmlBuilder.HtmlElement("p");
                ScormModel.HtmlBuilder.HtmlElement italicElement1 = new ScormModel.HtmlBuilder.HtmlElement("b");
                italicElement1.Nodes.Add((HtmlNode)new HtmlText("Module " + (i + 1).ToString()));
                descriptionElement.Nodes.Add((HtmlNode)italicElement1);
                data1Elelemt.Nodes.Add((HtmlNode)descriptionElement);
                row1Elelemt.Nodes.Add((HtmlNode)data1Elelemt);
                mainTableElement.Nodes.Add((HtmlNode)row1Elelemt);

                ScormModel.HtmlBuilder.HtmlElement row2Elelemt = new ScormModel.HtmlBuilder.HtmlElement("tr");
                ScormModel.HtmlBuilder.HtmlElement data2Elelemt = new ScormModel.HtmlBuilder.HtmlElement("td");

                ScormModel.HtmlBuilder.HtmlElement infoElement = new ScormModel.HtmlBuilder.HtmlElement("div");
                HtmlAttribute typeAttribute = new HtmlAttribute("id", "obj_info" + (i + 1).ToString());
                HtmlAttribute nameAttribute = new HtmlAttribute("name", "obj_info" + (i + 1).ToString());
                infoElement.Attributes.Add(typeAttribute);
                infoElement.Attributes.Add(nameAttribute);
                data2Elelemt.Nodes.Add((HtmlNode)infoElement);
                row2Elelemt.Nodes.Add((HtmlNode)data2Elelemt);
                mainTableElement.Nodes.Add((HtmlNode)row2Elelemt);

                ScormModel.HtmlBuilder.HtmlElement row3Elelemt = new ScormModel.HtmlBuilder.HtmlElement("tr");
                ScormModel.HtmlBuilder.HtmlElement data3Elelemt = new ScormModel.HtmlBuilder.HtmlElement("td");

                ScormModel.HtmlBuilder.HtmlElement statusElement = new ScormModel.HtmlBuilder.HtmlElement("div");
                HtmlAttribute statusId = new HtmlAttribute("id", "obj_statusBar" + (i + 1).ToString());
                HtmlAttribute statusName = new HtmlAttribute("name", "obj_statusBar" + (i + 1).ToString());
                statusElement.Attributes.Add(statusId);
                statusElement.Attributes.Add(statusName);
                data3Elelemt.Nodes.Add((HtmlNode)statusElement);
                row3Elelemt.Nodes.Add((HtmlNode)data3Elelemt);
                mainTableElement.Nodes.Add((HtmlNode)row3Elelemt);
            }
            inputForm.Nodes.Add((HtmlNode)mainTableElement);
        }

        private void WriteJavaScriptHtmlCode(ScormModel.HtmlBuilder.HtmlElement inputForm)
        {
            //write javascript html code
            ScormModel.HtmlBuilder.HtmlElement scriptElement = new ScormModel.HtmlBuilder.HtmlElement("script");
            HtmlAttribute languageAttribute = new HtmlAttribute("language", "javascript");
            HtmlText javaScriptCode = new HtmlText(this.CreateJavaScriptCode(inputForm));
            scriptElement.Attributes.Add(languageAttribute);
            scriptElement.Nodes.Add((HtmlNode)javaScriptCode);
            inputForm.Nodes.Add((HtmlNode)scriptElement);
        }

        private string CreateJavaScriptCode(ScormModel.HtmlBuilder.HtmlElement inputForm)
        {
            string Res = "\nvar tempIndex;\nvar tempObjScore;\nvar tempVal;\nvar msg=\"\";\n";
            for (int i = 0; i < listObjective.Count; i++)
            {
                Res += "\nvar obj_scaledScore" + (i + 1).ToString()+";";
            }
            Res += "\nvar exitPageStatus;\nfunction loadPage()\n{\ndoInitialize();\nexitPageStatus = false;\ngetObjectivesStatus();\nwriteObjectivesStatus();\n}\n";
            Res += "\nfunction unloadPage()\n{\nif(exitPageStatus != true)\n{\ndoQuit();\n}\n}\n";
            Res += "\nfunction doQuit()\n{\ndoSetValue( \"cmi.completion_status\", \"completed\" );\ndoSetValue( \"cmi.exit\", \"\" );\nexitPageStatus = true;\nvar result = doTerminate();\n}\n";
            Res += "\nfunction findObjective(obj)\n{\nvar numOfObj = doGetValue(\"cmi.objectives._count\");\nvar objectiveLocation = -1; ";
            Res += "\nfor ( var i=0; i < numOfObj; i++ ) \n";
            Res += "{ \nif ( doGetValue(\"cmi.objectives.\" + i + \".id\") == obj )\n{\nobjectiveLocation = i;\nbreak;\n}\n}\n";
            Res += "\nif(objectiveLocation == -1 )\n{\n alert(\"objective not found\");\nobjectiveLocation = numOfObj;\ndoSetValue( \"cmi.objectives.\" + objectiveLocation + \".id\", obj);\n}";
            Res += "\nreturn objectiveLocation;";
            Res += "\n}\n";
            Res += "\nfunction stripDecimal( ioPercent )\n{\nvar stringIoPercent = String( ioPercent);\nvar decimalIndex = stringIoPercent.indexOf(\".\");\nvar returnVal;\n";
            Res += "\nif ( ! (decimalIndex == -1) )\n{\nreturnVal = stringIoPercent.substring(0,decimalIndex);\n}\nelse \n{\nreturnVal = stringIoPercent;\n}\nreturn returnVal;";
            Res += "\n}\n";
            Res += "\nfunction getObjectivesStatus()\n{";
            for (int i = 0; i < listObjective.Count; i++)
            {
                Res += "\ntempIndex = findObjective(\"" + listObjective[i] + "\")\n";
                Res += "\ntempObjScore = \"cmi.objectives.\" + tempIndex + \".score.scaled\";\n";
                Res += "\nobj_scaledScore" + (i + 1).ToString() + "=doGetValue(tempObjScore);\n";
                Res += "\nif(obj_scaledScore" + (i + 1).ToString() + " == null) \n{\nobj_scaledScore" + (i + 1).ToString() + " = 0;\n}\n";
            }
            Res += "}";

            Res += "\nfunction writeObjectivesStatus()\n{";
            for(int i =0;i<listObjective.Count;i++)
            {
                Res += "\ntempVal = obj_scaledScore" + (i + 1).ToString() +" * 100; \ntempVal = stripDecimal(tempVal);\n";
                Res += "\ndocument.all.obj_info" + (i + 1).ToString() + ".innerHTML = tempVal + \"%\";\n";
                Res += "\nfor( i=0; i < 100; i=i+5 )\n{";
                Res += "\nif(i < tempVal)\n{\nmsg += \"<img src='Images/gbar.gif' />\"\n}";
                Res += "\nelse{\n msg += \"<img src='Images/bbar.gif' />\"\n}";
                Res += "\n}";
                Res += "\ndocument.all.obj_statusBar" + (i + 1).ToString() + ".innerHTML = msg";
                Res += "\nmsg=\"\"";
            }
            Res += "\n}";
            return Res;
        }
    }
}
